home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / ndx300.zip / TEST.CPP < prev   
C/C++ Source or Header  |  1992-10-27  |  3KB  |  119 lines

  1. //        Test NDX v3.0
  2.  
  3. #define TEST                        // Makes Key public
  4. #include     "ndx.h"
  5. #include    <stdlib.h>
  6.  
  7. void Index::print(char *filename)
  8. {
  9.     Node *node = new Node;
  10.     unsigned long *blocks;
  11.     int cblock = 0, last_block = 1;
  12.     FILE *file = fopen(filename? filename: "CON", "w");
  13.  
  14.     blocks =(unsigned long *)calloc(20, sizeof(long));
  15.     *blocks = root;
  16.     if (! *blocks) {
  17.         fputs("Empty index\n", file);
  18.         return;
  19.     }
  20.     for (; cblock < last_block; cblock++) {
  21.         Key *kp, *end;
  22.  
  23.         read(blocks[cblock], node, nodesize);
  24.         kp = (Key *)node->keys;
  25.         end = (Key *)(node->keys + node->endkeys);
  26.  
  27.         fprintf(file, "\nBlock at %lu\n", blocks[cblock]);
  28.         for (; kp < end; (char *)kp += 2*sizeof(long) + strlen(kp->data)
  29.             + 1) {
  30.             if (kp->lson)
  31.                 blocks[last_block++] = kp->lson;
  32.             fprintf(file, "         %-8lu %-8s %-8lu\n",
  33.                 kp->lson, kp->data, kp->offset);
  34.         }
  35.         if (kp->lson) {
  36.             blocks[last_block++] = kp->lson;
  37.             fprintf(file, "         %-8lu\n", kp->lson);
  38.         }
  39.     }
  40.     free(node);
  41.     free(blocks);
  42.     fclose(file);
  43. }
  44.  
  45. Index *index;
  46. Key *entry = new Key;
  47. int seed = 16383;
  48. unsigned rval[10000];
  49.  
  50. void output()
  51. {
  52.     index->print("t.tmp");
  53. }
  54.  
  55. void check(int i, int j, int last)
  56. {
  57.     while (j--) {
  58.         printf("%5u\r", j);
  59.         if (! i++) index->first(last);
  60.         else if (! (last? index->prev(): index->next())) {
  61.             printf("Key <%u, %u> not found\n", last? j + 1: i,
  62.                     rval[last? j: i - 1]);
  63.             output();
  64.             exit(-1);
  65.             }
  66.         }
  67.     printf("All keys accessible on %s scan\n", last? "backward":
  68.             "forward");
  69. }
  70.  
  71. main(int argc, char **argv)
  72. {
  73.     int n = atoi(argv[1]), i, j, m = (argc >= 2? atoi(argv[2]): 0);
  74.     int fault = 0;
  75.  
  76.     if (argc < 2) {
  77.         puts("\n\tUsage: NDX <number of keys> [<modulus>] [print]\n\n"
  78.             "\tAny third argument causes the index to be printed\n"
  79.             "\tUse of the second argument exercises the ISAM feature\n"
  80.             "\n\tTry NDX 200 10 0, for instance\n");
  81.         exit(-1);
  82.         }
  83.     srand(seed);
  84.     for (i = 0; i < n; i++) rval[i] = m? rand() % m: rand() + 1;
  85.     index = new Index("T.NDX", FIFO);
  86.     entry->lson = 0L;
  87.     for (i = 1; i <= n; i++) {
  88.         sprintf(entry->data, "%u", rval[i - 1]);
  89.         entry->offset = i;
  90.         index->insert(entry->data, entry->offset);
  91.         }
  92.     puts("Keys written");
  93.     delete index;
  94.     index = new Index("t.ndx");
  95.     output();
  96.     check(0, n, 0);
  97.     check(0, n, 1);
  98.     if (argc > 3) {index->print(NULL); return -1; }
  99.     for (i = 1; i <= n; i++) {
  100.         sprintf(entry->data, "%u", rval[i - 1]);
  101.         entry->offset = i;
  102.         if (! index->remove(entry->data, entry->offset)) {
  103.             printf("Delete failure: record %-u, data %-s\a\a\n", i, entry->data);
  104.             output();
  105.             fault++;
  106.             break;
  107.             }
  108.           else if (index->findkeyrec(entry->data, entry->offset)) {
  109.             printf("Key <%u, %u> not deleted!\n", i, rval[i - 1]);
  110.             output();
  111.             fault++;
  112.             break;
  113.             }
  114.     }
  115.     delete index;
  116.     if (! fault) puts("All keys successfully deleted");
  117.     return 0;
  118. }
  119.